package wu.controller;

import org.apache.ibatis.session.SqlSession;
import wu.mapper.StudentMapper3;
import wu.pojo.NiuStudent;
import wu.util.MyBatisUtils;

import java.util.*;

public class getHTML_js_data3 {
    private static final SqlSession sqlSession = MyBatisUtils.getSession();
    private static final StudentMapper3 studentMapper = sqlSession.getMapper(StudentMapper3.class);
    private static List<NiuStudent> list = new ArrayList<>();
    private static List<NiuStudent> list_oid = new ArrayList<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要输出html数据的本周的周赛周数：");
//        int num = 71;
        int num = sc.nextInt();
        list = setListNiuStudent(num, list);
        System.out.println("\n\n\n\n\n\n");
        list_oid = setListNiuStudent(num - 1, list_oid);
        List<NiuStudent> ll;
        // 初始化数组
        int[] listsize = new int[7];

// 赋值
        listsize[0] = list.size();
        listsize[1] = list_oid.size();
        for (int i = 2; i < listsize.length; i++) {
            ll = new ArrayList<>();
            listsize[i] = setListNiuStudent(num - i, ll).size();
        }
        System.out.println("\n\n\n\n\n\n" + "<script type=\"text/javascript\">");
        System.out.println("const num= " + num + ";");
        left2(num, list);
        left3(num);
        center1(num, list);
        center2(num);
        right1(num);
        right2(num, listsize);
        right3(num);
        System.out.println("</script>");
        sc.close();
        sqlSession.close();
    }

    private static void left2(int num, List<NiuStudent> list) {
        int len = 7;
        double[] values = new double[len * 2];
        for (NiuStudent niuStudent1 : list) {
            if (niuStudent1.getA_score() != -1) {
                values[0] += niuStudent1.getA_score();
            }
            if (niuStudent1.getB_score() != -1) {
                values[1] += niuStudent1.getB_score();
            }
            if (niuStudent1.getC_score() != -1) {
                values[2] += niuStudent1.getC_score();
            }
            if (niuStudent1.getD_score() != -1) {
                values[3] += niuStudent1.getD_score();
            }
            if (niuStudent1.getE_score() != -1) {
                values[4] += niuStudent1.getE_score();
            }
            if (niuStudent1.getF_score() != -1) {
                values[5] += niuStudent1.getF_score();
            }
            if (niuStudent1.getG_score() != -1) {
                values[6] += niuStudent1.getG_score();
            }
        }
        System.out.print("const left2= [");
        for (int ii = 0; ii < len; ii++) {
//            System.out.print("{value:" + values[ii] + ",name: '" + (char) ('A' + ii) + "'}");
            System.out.print("{value:" + String.format("%.2f", values[ii]) + ",name: '" + (char) ('A' + ii) + "'}");

            if (ii != len - 1) {
                System.out.println(",");
            }
        }
        System.out.println("];");
    }

    private static void left3(int num) {
        int len = 7;
        int[] arr1 = new int[len], arr2 = new int[len], arr3 = new int[len];
        for (NiuStudent niuStudent1 : list_oid) {
            if (niuStudent1.getB_score() != -1) {
                arr1[1]++;
            }
            if (niuStudent1.getC_score() != -1) {
                arr1[2]++;
            }
            if (niuStudent1.getD_score() != -1) {
                arr1[3]++;
            }
            if (niuStudent1.getE_score() != -1) {
                arr1[4]++;
            }
            if (niuStudent1.getF_score() != -1) {
                arr1[5]++;
            }
            if (niuStudent1.getG_score() != -1) {
                arr1[6]++;
            }
            if (niuStudent1.getA_score() != -1) {
                arr1[0]++;
            }
        }
        for (NiuStudent niuStudent : list) {
            if (niuStudent.getB_score() != -1) {
                arr2[1]++;
            }
            if (niuStudent.getC_score() != -1) {
                arr2[2]++;
            }
            if (niuStudent.getD_score() != -1) {
                arr2[3]++;
            }
            if (niuStudent.getE_score() != -1) {
                arr2[4]++;
            }
            if (niuStudent.getF_score() != -1) {
                arr2[5]++;
            }
            if (niuStudent.getG_score() != -1) {
                arr2[6]++;
            }
            if (niuStudent.getA_score() != -1) {
                arr2[0]++;
            }
        }
        for (int i = 0; i < len; i++) {
            arr3[i] = arr2[i] - arr1[i];
        }
        System.out.print("const left3_data1= [");
        for (int ii = 0; ii < len; ii++) {
            System.out.print("'" + arr1[ii] + "'");
            if (ii != len - 1) {
                System.out.print(",");
            }
        }
        System.out.println("];");
        System.out.print("const left3_data2= [");
        for (int ii = 0; ii < len; ii++) {
            System.out.print("'" + arr2[ii] + "'");
            if (ii != len - 1) {
                System.out.print(",");
            }
        }
        System.out.println("];");
        System.out.print("const left3_data3= [");
        for (int ii = 0; ii < len; ii++) {
            System.out.print("'" + arr3[ii] + "'");
            if (ii != len - 1) {
                System.out.print(",");
            }
        }
        System.out.println("];");
    }

    private static void center1(int num, List<NiuStudent> list) {
        int len = 7;
        double[] values = new double[len * 2];
        for (NiuStudent niuStudent1 : list) {
            if (niuStudent1.getA_score() != -1) {
                values[0] += niuStudent1.getA_score();
            }
            if (niuStudent1.getB_score() != -1) {
                values[1] += niuStudent1.getB_score();
            }
            if (niuStudent1.getC_score() != -1) {
                values[2] += niuStudent1.getC_score();
            }
            if (niuStudent1.getD_score() != -1) {
                values[3] += niuStudent1.getD_score();
            }
            if (niuStudent1.getE_score() != -1) {
                values[4] += niuStudent1.getE_score();
            }
            if (niuStudent1.getF_score() != -1) {
                values[5] += niuStudent1.getF_score();
            }
            if (niuStudent1.getG_score() != -1) {
                values[6] += niuStudent1.getG_score();
            }
        }
        System.out.println("const center1= [");
        String[] names = {"A", "B", "C", "D", "E", "F", "G", "", "", "", "", "", "", ""};
        String[] colors = {"#f845f1", "#ad46f3", "#5045f6", "#4777f5", "#44aff0", "#45dbf7", "#f6d54a", "transparent", "transparent", "transparent", "transparent", "transparent", "transparent", "transparent"};
        for (int i = 0; i < len; i++) {
            System.out.print("{value: ");
            System.out.printf("%.2f", values[i]);
            System.out.print(",name:\'");
            System.out.print(names[i]);
            System.out.print("\',itemStyle: {normal: {color:\'");
            System.out.print(colors[i]);
            System.out.print("\'}}}");
            System.out.println(",");
        }
        for (int i = len; i < len * 2; i++) {
            System.out.print("{value: ");
            System.out.print(values[i]);
            System.out.print(",name:\'");
            System.out.print(names[i]);
            System.out.print("\',itemStyle: {normal: {color:\'");
            System.out.print(colors[i]);
            System.out.print("\'}},label: {show: false},labelLine: {show: false}}");
            if (len * 2 - 1 != i)
                System.out.println(",");
        }
        System.out.println("];");

    }

    public static void center2(int num) {
        int len = list.size();
        System.out.println("const center2= [");
        for (int ii = 0; ii < len; ii++) {
            System.out.print("[\'");
            System.out.print(ii + 1);
            System.out.print("\',\'");
            System.out.print(list.get(ii).getClassInfo());
            System.out.print("\',\'");
            System.out.print(list.get(ii).getName());
            System.out.print("\',\'");
            System.out.print(list.get(ii).getNiu_Contestants());
            System.out.print("\',\'");
            System.out.print(list.get(ii).getNiu_id());
            System.out.print("\',\'");
            System.out.print(list.get(ii).getNiu_Ranking());
            System.out.print("\',\'");
            System.out.print(list.get(ii).getNiu_School());
            System.out.print("\',\'");
            System.out.print(list.get(ii).getNiu_AC());
            System.out.print("\',\'");
            System.out.print(list.get(ii).getNiu_total_score());
            System.out.print("\',\'");
            System.out.print(list.get(ii).getNiu_time());
            System.out.print("\']");
            if (ii != len - 1) {
                System.out.println(",");
            }
        }
        System.out.println();
        System.out.println("];");

        System.out.println("const center2_yidong= [");
        for (int ii = 0; ii < len; ii++) {
            System.out.print("[\'");
            System.out.print(ii + 1);
            System.out.print("\',\'");
            System.out.print(list.get(ii).getClassInfo());
            System.out.print("\',\'");
            System.out.print(list.get(ii).getName());
            System.out.print("\',\'");
            System.out.print(list.get(ii).getNiu_AC());
            System.out.print("\',\'");
            System.out.print(list.get(ii).getNiu_total_score());
            System.out.print("\']");
            if (ii != len - 1) {
                System.out.println(",");
            }
        }
        System.out.println();
        System.out.println("];");

    }

    private static void right1(int num) {
        int len = 7;
        int[] arr1 = new int[len], arr2 = new int[len], arr3 = new int[len];
        for (NiuStudent niuStudent1 : list) {
            if (niuStudent1.getB_score() != -1) {
                arr1[1]++;
            }
            if (niuStudent1.getC_score() != -1) {
                arr1[2]++;
            }
            if (niuStudent1.getD_score() != -1) {
                arr1[3]++;
            }
            if (niuStudent1.getE_score() != -1) {
                arr1[4]++;
            }
            if (niuStudent1.getF_score() != -1) {
                arr1[5]++;
            }
            if (niuStudent1.getG_score() != -1) {
                arr1[6]++;
            }
            if (niuStudent1.getA_score() != -1) {
                arr1[0]++;
            }
        }
        System.out.println("const right1= [");
        String[] names = {"A", "B", "C", "D", "E", "F", "G"};
        for (int i = 0; i < len; i++) {
            System.out.print("{value: " + arr1[i]);
            System.out.print(",name: '" + names[i] + "'}");
            if (i != len - 1) {
                System.out.println(",");
            }
        }
        System.out.println("];");
    }

    private static void right2(int num, int[] listsize) {
        System.out.print("const right2_data1= [");
        for (int ii = 0; ii < 7; ii++) {
            System.out.print("\'" + (num - 6 + ii) + "周\'");
            if (ii != 6) {
                System.out.print(",");
            }
        }
        System.out.println("];");
        System.out.print("const right2_data2= [");
//        for (int i = 0; i < 7; i++) {
        for (int i = 6; i >= 0; i--) {
            System.out.print(listsize[i]);
            if (i != 0) {
                System.out.print(",");
            }
        }
        System.out.println("];");
    }

    private static void right3(int num) {
        System.out.println("const right3= [");
        int len = list_oid.size();
        for (int ii = 0; ii < len; ii++) {
            System.out.print("[\'");
            System.out.print(ii + 1);
            System.out.print("\',\'");
            for (int i1 = 0; i1 < list.size(); i1++) {
                if (list.get(i1).getNiu_id() == list_oid.get(ii).getNiu_id()) {
                    if (i1 == ii) {
                        System.out.print("⊜");
                    } else if (i1 < ii) {
                        System.out.print("⇪");
                    } else {
                        System.out.print("⇩");
                    }
                    break;
                }
                if (i1 == list.size() - 1) {
                    System.out.print("⌀");
                }
            }
            System.out.print("\',\'");
            System.out.print(list_oid.get(ii).getClassInfo());
            System.out.print("\',\'");
            System.out.print(list_oid.get(ii).getName());
            System.out.print("\',\'");
            System.out.print(list_oid.get(ii).getNiu_total_score());
            System.out.print("\',\'");
            System.out.print(list_oid.get(ii).getNiu_time());
            System.out.print("\']");
            if (ii != len - 1) {
                System.out.println(",");
            }
        }
        System.out.println();
        System.out.println("];");

    }

    private static List<NiuStudent> setListNiuStudent(int num, List<NiuStudent> list2) {
        List<Integer> niuIds = studentMapper.getNiuIds();
        for (Integer niuId : niuIds) {
            NiuStudent niuStudent = studentMapper.getStudentByIdAndTableName3("user_table_" + num, niuId);
            // 处理查询到的学生和相关 User_niu 信息
            if (niuStudent != null) {
                list2.add(niuStudent);
            }
        }
        // 创建一个Comparator接口实现，用于比较两个NiuStudent对象的niu_total_score属性
        Comparator<NiuStudent> comparator = new Comparator<NiuStudent>() {
            @Override
            public int compare(NiuStudent s1, NiuStudent s2) {
                if (s1.getNiu_total_score() == null) {
                    return 1;
                }
                if (s2.getNiu_total_score() == null) {
                    return -1;
                }
                // 将字符串转换为double类型后进行比较
                double score1 = Double.parseDouble(s1.getNiu_total_score());
                double score2 = Double.parseDouble(s2.getNiu_total_score());
                if (score2 > score1) {
                    return 1;
                } else if (score2 < score1) {
                    return -1;
                } else {
                    return s1.getNiu_time().compareTo(s2.getNiu_time());
                }
            }
        };
        // 使用Collections.sort方法对列表进行排序
        Collections.sort(list2, comparator);
        return list2;
    }
}
